#coding=utf-8
import socket
import xml
import xml.etree.ElementTree as ET

serverHost = "118.26.23.97"
serverPort = 7779


removDevXml = "<Root>\
  <Pat>\
    <AddNewDev>\
      <ReqType>S32,0,256|0</ReqType>\
      <ReqAccount>STR,5|admin</ReqAccount>\
      <DevId>STR,8|ts000001</DevId>\
      <AreaId>S32,0,65535|0</AreaId>\
      <DevOwnerId>STR,7|develop</DevOwnerId>\
      <UserData>STR,8|105cfdb0</UserData>\
      <Err/>\
    </AddNewDev>\
  </Pat>\
</Root>"

def xml_set_tag(root,name,value):
    for node in root.iter(name):
        node.text = str(value)


def remove_dev_user():
    print(removDevXml)
    root = ET.fromstring(removDevXml)
    xml_set_tag(root,"ReqType","test")
    root.write("bound1.xml")
    xmlfile = open("bound1.xml")
    xmlstring = xmlfile.read()
    #root.write(removDevXml)
    print(xmlstring)

def createSocket():
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect((serverHost,serverPort))
    return s

s = createSocket()

def getxmlremoveMsg(id,user):
    tree = ET.parse("unbound.xml")
    root = tree.getroot()

    for node in root.iter('DevId'):
        data = "STR,8|"+id
        node.text = str(data)
        tree.write("unbound.xml")
        file = open('unbound.xml')
        xml = file.read()
        return xml

def getXmlSend(id,user):
    tree = ET.parse("bound.xml")
    root = tree.getroot()

    for node in root.iter('DevId'):
        new_data = "STR,8|"+id
        node.text = str(new_data)

    for node in root.iter('Alias'):
        new_data = "STR,11|cms"+id
        node.text = str(new_data)

    for node in root.iter('DevOwnerId'):
        new_data = "STR,12|"+user
        node.text = str(new_data)

    '''
    for node in root.iter('DevId'):
        new_data = "STR,7|"+user
        node.text = str(new_data)
    '''

    tree.write("bound.xml")
    xmlfile = open("bound.xml")
    xmlstring = xmlfile.read()
    return xmlstring

def del_dev_touser():
    #循环发送绑定信息
    file = open("111.data","r")
    for line in range(0,900):
        id = file.readline(9)
        #print(id)
        #发送报文
        sdata = getxmlremoveMsg(id,"develop")

        sdata = sdata.replace("\n","")
        #sdata = sdata.replace("","")
        #print(len(sdata))
        lenstr = "%04d" %len(sdata)
        #print(lenstr)
        endstr = "%04d" %len(sdata)
        #print(endstr)
        send = b'!paT00f2XMLD000000000000'+lenstr.encode()+b'00000000'+lenstr.encode()+sdata.encode()+ endstr.encode()
        print(">>>",send)
        s.send(send)
        data = s.recv(1024)
        print("<<<",data)


def add_dev_touser():
    #循环发送绑定信息
    file = open("DevID.txt","r")
    uflie = open("UserID.txt","r")
    for line in range(0,1050):
        id = file.readline(11)
        user = uflie.readline(12)
        print(id)
        print(user)
        #发送报文
        sdata = getXmlSend(id,user)

        sdata = sdata.replace("\n","")
        #sdata = sdata.replace("","")
        #print(len(sdata))
        lenstr = "%04d" %len(sdata)
        #print(lenstr)
        endstr = "%04d" %len(sdata)
        #print(endstr)
        send = b'!paT00f2XMLD000000000000'+lenstr.encode()+b'00000000'+lenstr.encode()+sdata.encode()+ endstr.encode()
        print(">>>",send)
        s.send(send)
        data = s.recv(1024)
        print("<<<",data)

    file.close()

def create_users():
    user_id = 1
    for i in range(0,1050):
        user_id = int(user_id) + 1
        user_id = str(user_id)
        send = b'!paT00f1XMLD0000000000001051000000001051<Root><Pat><ModifyAccountInfo><ReqAccount>STR,9|testadmin</ReqAccount><AccountType>S32,0,65535|5</AccountType><AccountId>STR,10|GD1906'+user_id.zfill(4).encode()+b'</AccountId><Alias>STR,13|cmsGD1906'+user_id.zfill(4).encode()+b'</Alias><Psw>PWD,6|123456</Psw><AreaId>S32,0,65535|29</AreaId><StartTime>STR,19|1970-01-01 00:00:00</StartTime><EndTime>STR,19|2022-05-06 02:57:51</EndTime><Email>STR,8|1@qq.com</Email><LinkMan1>STR,1|1</LinkMan1><LinkMan2>STR,0|</LinkMan2><LinkMan1Tel>STR,1|1</LinkMan1Tel><LinkMan2Tel>STR,0|</LinkMan2Tel><LinkMan1Mobile>STR,1|1</LinkMan1Mobile><LinkMan2Mobile>STR,0|</LinkMan2Mobile><LinkMan1Addr>STR,1|1</LinkMan1Addr><LinkMan2Addr>STR,0|</LinkMan2Addr><Remarks>STR,0|</Remarks><UserPoint>S32,0,65535|0</UserPoint><SmsCnt>S32,0,65535|0</SmsCnt><BusinessType>S32,0,65535|0</BusinessType><DevLocation>STR,0|</DevLocation><Longitude>STR,8|0.000000</Longitude><Latitude>STR,8|0.000000</Latitude><IsAllowAlarm>S32,0,255|0</IsAllowAlarm><IsAllowSecDev>BOL|F</IsAllowSecDev><IsAllowDev>BOL|F</IsAllowDev><UserData>STR,7|8167930</UserData><Err/></ModifyAccountInfo></Pat></Root>1051'
        print(send)
        s.send(send)
        data = s.recv(2048)
    pass


if __name__ == "__main__":
    print("main")
    '''
    cmd = input("输入1删除所有用户与设备关系:")
    if cmd == '1':
        #remove_dev_user()
        del_dev_touser()
    elif cmd == '2':
        add_dev_touser()
    '''
    #create_users()#创建用户
    add_dev_touser()

